<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'html_generator.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_roulette.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';
$roulette_id = filter_input(INPUT_POST, "roulette_id");

$roulette = api_roulette::get_roulette_by_id($roulette_id);
//var_dump(lib_date::get_datetime_now());
//var_dump(lib_date::convert_to_datetime($roulette['date_end'], "Y-m-d H:i:s"));

$result = 1;
$win_choice_id = api_roulette::get_win_choice_id($roulette_id);
if (empty($win_choice_id)) {
    $result = api_roulette::random_result();
} else {
    $result = api_roulette::get_roulette_map_by_map_id($win_choice_id);
}

if (lib_date::convert_to_datetime($roulette['date_end'], "Y-m-d H:i:s") <= lib_date::get_datetime_now()
        && empty($win_choice_id)) {
    api_roulette::save_roulette_result($roulette_id, $result);
}
echo json_encode($result);
